/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.iss.examsys.view;

import com.iss.examsys.ejb.AddExamQuestionBean;
import com.iss.examsys.ejb.ModuleBean;
import com.iss.examsys.ejb.QuestionBean;
import com.iss.examsys.ejb.SubjectTagBean;
import com.iss.examsys.entity.AnswerChoice;
import com.iss.examsys.entity.LecturerModule;
import com.iss.examsys.entity.Module;
import com.iss.examsys.entity.Question;
import com.iss.examsys.entity.QuestionPart;
import com.iss.examsys.entity.QuestionTag;
import com.iss.examsys.entity.SubjectTag;
import com.iss.examsys.entity.User;
import com.iss.examsys.view.util.JsfUtil;
import static com.sun.jersey.spi.inject.Errors.mark;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.SelectItem;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author teng.hui.pei
 */
@Named
@ViewScoped
public class AddExamQuestionView implements Serializable {

    @EJB private QuestionBean questionBean;
    @EJB private ModuleBean moduleBean;
    @EJB private AddExamQuestionBean addExamQuestionBean; 
    @EJB private SubjectTagBean subjectTagBean;
    // View Bean
    private List<Module> moduleList;
    private List<SubjectTag> subjectTagList;
    private SelectItem[] selectQuestionTypes;
    
    private String questionType;
    
    // Entity bean
    private User user;
    private Module module;
    private LecturerModule lectureModule;
    private QuestionTag questionTag;
    private Question question;
    private AnswerChoice answerChoice ;
    private QuestionPart questionPart;
    private SubjectTag subjectTag;

    public AddExamQuestionView() {
        
        user = new User();
        module = new Module();
        lectureModule = new LecturerModule();
        questionTag = new QuestionTag();
        question = new Question();
        answerChoice = new AnswerChoice();
        questionPart = new QuestionPart();
        subjectTag = new SubjectTag();
        
    }
    public void addQuestion(){
       System.out.println(">>> test:"+questionType);
    }

    public String getQuestionType() {
        return questionType;
    }

    public void setQuestionType(String questionType) {
        this.questionType = questionType;
    }
    
    public SelectItem[] getSelectQuestionTypes() {
        return JsfUtil.getSelectQuestionTypeItems();
    }

    public void setSelectQuestionTypes(SelectItem[] selectQuestionTypes) {
        this.selectQuestionTypes = selectQuestionTypes;
    }

        //autocomplete completeMethod
    public List<SubjectTag> completeSubjectTags(String query) {
        List<SubjectTag> result = new LinkedList<>();
        List<SubjectTag> tags = subjectTagBean.findAll();
        
        for(SubjectTag tag : tags) {
            if(tag.getName().toLowerCase().contains(query.toLowerCase())) {
                result.add(tag);
            }
        }        
        return (result);
    }  
    
    public List<SubjectTag> getSubjectTagList() {
        return subjectTagList;
    }

    public void setSubjectTagList(List<SubjectTag> subjectTagList) {
        this.subjectTagList = subjectTagList;
    }

//        private List<Module> getModulesForLecturer() {      
//        List<LecturerModule> lecturerModules;
//        List<Module> lecModules = new LinkedList<>();
//
//        ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
//        HttpServletRequest req = (HttpServletRequest)ctx.getRequest();
//        String userId = req.getRemoteUser();
//        
//        User user = userBean.findByUserId(userId);
//        
//        if(user != null) {
//            lecturerModules = user.getLecturerModuleList();
//            if(lecturerModules != null) {
//                for(LecturerModule lm : lecturerModules){
//                    lecModules.add(lm.getModuleId());
//                } 
//            }
//        }
//        return lecModules;
//    }
    
    public List<Module> getModuleList() {
        moduleList = addExamQuestionBean.findAllModuleForLectureId(2);
        return moduleList;
    }
    
    

    public void setModuleList(List<Module> moduleList) {
        this.moduleList = moduleList;
    }

    
    public void saveQuestion(){
        
        question = new Question();
    }
    
    public SubjectTag getSubjectTag() {
        return subjectTag;
    }

    public void setSubjectTag(SubjectTag subjectTag) {
        this.subjectTag = subjectTag;
    }

    
    
    public Module getModule() {
        return module;
    }

    public void setModule(Module module) {
        this.module = module;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public LecturerModule getLectureModule() {
        return lectureModule;
    }

    public void setLectureModule(LecturerModule lectureModule) {
        this.lectureModule = lectureModule;
    }

    public QuestionTag getQuestionTag() {
        return questionTag;
    }

    public void setQuestionTag(QuestionTag questionTag) {
        this.questionTag = questionTag;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public AnswerChoice getAnswerChoice() {
        return answerChoice;
    }

    public void setAnswerChoice(AnswerChoice answerChoice) {
        this.answerChoice = answerChoice;
    }

    public QuestionPart getQuestionPart() {
        return questionPart;
    }

    public void setQuestionPart(QuestionPart questionPart) {
        this.questionPart = questionPart;
    }
    
    
    
}
